/*
	Author:
		Hubert Seales
	Creation Date:
		11/20/2007
	Description:
		This file checks for notifications by executing a GET on the xmlURL.
	Changelog
		0.1:	Initial release
		0.2:	Checking for inbox id existence BEFORE polling
*/
/* *********************************** <CONFIG> *********************************** */
// php script that displays the notifications in xml.
// Be advised that this url shouldn't contain a domain...
// it's best to keep this one relative due to ajax security restrictions
var xmlURL = "/serv/notify.php";

// maximum number of minutes this script should continually poll for
var maxMinutes	= 5;
/* *********************************** </CONFIG> ********************************** */

var doNotify	= 1;

function doClick() {
	js.executeMe();
}

function startNotify() {
	// If the inbox div doesn't exist, DO NOT poll, this means that the page has no leftnav
	var inboxDiv	= document.getElementById('inbox');
	if( inboxDiv != null) {
		mytime=setInterval('loadXML()', 5000);
		var stopTime	= (maxMinutes * 60) * 1000;
		mytime=setTimeout('stopNotify()', stopTime);
	}
}

function stopNotify() {
	doNotify	= 0;
}

function parseXML(xmlString) {
	if (window.ActiveXObject) {
		var xmlDoc		= new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async	= "false";
		xmlDoc.loadXML(xmlString);
	} else if (document.implementation && document.implementation.createDocument) {
		var parser	= new DOMParser();
		var xmlDoc	= document.implementation.createDocument("ns","root",null);
		var xmlDoc	= parser.parseFromString(xmlString,"text/xml");

	} else {
		alert("Ajax not supported.");
	}

	var xmlObj		= xmlDoc.documentElement; 

	// display the raw xml for IE only
	//alert(xmlObj.xml);


	// If there are no child nodes, the server sent nothing, cease here
	if(xmlObj.childNodes.length == 0) {
		return;
	}

	for( var x=0; x<xmlObj.childNodes.length; x++ ) {
		var tagVal	= xmlObj.childNodes[x].childNodes[0].nodeValue;
		var tagKey	= xmlObj.childNodes[x].tagName;

		if( tagVal > 0 ) {
			document.getElementById(tagKey).innerHTML	= "[new]";
		} else {
			document.getElementById(tagKey).innerHTML	= "";
		}
	}

}

function loadXML() { 
	if( doNotify == 0 ) return;

	if(navigator.appName == "Microsoft Internet Explorer") {
		http = new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		http = new XMLHttpRequest();
	}
	http.open("GET", xmlURL);
	http.onreadystatechange=function() {
		if(http.readyState == 4) {
			parseXML(http.responseText);
		}
	}
	http.send(null);

} // loadXML

